package gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JFrame;

import map.Map;
import map.Region;
import map.Tile;
import entity.Player;


@SuppressWarnings("serial")
public class View extends JFrame {

	public static final int TILES = 100;
	
	/*
	 * In the future it will be a constant
	 */
	public static int TILE_SIZE = 32;

	private final int WIDTH = 500;
	private final int HEIGHT = 500;
	
	//TODO: Must use the Map parts to draw as well
	private Tile[][] tile = new Tile[TILES][TILES];

	private int[] mapPosition = {0,0};

	private Player player = new Player(this);

	private Object[] fade = {false, 0};

	private Map map = new Map();

	public View() {
		setSize(WIDTH,HEIGHT);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Listener listener = new Listener(this);
		addMouseWheelListener(listener);
		addMouseMotionListener(listener);
		addMouseListener(listener);
		addKeyListener(listener);
		tile = map.getRegion(1, 1).getLayer(0).getTiles();
		player.setTile(tile[1][1]);
		player.setLayer(map.getRegion(1, 1).getLayer(0));
		setVisible(true);
	}

	@Override
	public void paint(Graphics g2) {
		Image offscreen = createImage(getWidth(),getHeight());
		Graphics g = offscreen.getGraphics();
		g.setColor(Color.GRAY);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setColor(Color.WHITE);
		g.fillRect(mapPosition[0], mapPosition[1], TILES*TILE_SIZE, TILES*TILE_SIZE);
		for (int i = 0; i < TILES; i++) {
			for (int j = 0; j < TILES; j++) {
				g.drawImage(tile[i][j].getProperties().getType(),
						mapPosition[0]+(i*TILE_SIZE), mapPosition[1]+(j*TILE_SIZE),
						TILE_SIZE, TILE_SIZE, null);
				/*g.setColor(tile[i][j].getProperties().getType());
				g.fillRect(mapPosition[0]+(i*tileSize), mapPosition[1]+(j*tileSize),
						tileSize, tileSize);*/
				/*if (tile[i][j].getProperties().getBlocked()) {
					g.setColor(Color.RED);
					g.drawLine(mapPosition[0]+(i*tileSize), mapPosition[1]+(j*tileSize),
							(mapPosition[0]+(i*tileSize))+tileSize,
							(mapPosition[1]+(j*tileSize))+tileSize);
				}*/
			}
		}
		/*g.setColor(Color.BLACK);
		for (int i = 0; i < TILES; i++) {	
			g.drawLine(mapPosition[0]+(i*tileSize), mapPosition[1],
				mapPosition[0]+(i*tileSize), mapPosition[1]+(TILES*tileSize));
			g.drawLine(mapPosition[0], mapPosition[1]+(i*tileSize),
					 mapPosition[0]+(TILES*tileSize), mapPosition[1]+(i*tileSize));
		}*/
		g.setColor(Color.RED);
		g.fillOval(mapPosition[0]+player.getPosition()[0],
				mapPosition[1]+player.getPosition()[1],
				TILE_SIZE, TILE_SIZE);
		if ((Boolean) fade[0]) {
			g.setColor(new Color(0,0,0,(Integer) fade[1]));
			g.fillRect(0, 0, getWidth(), getHeight());
		}
		g2.drawImage(offscreen, 0, 0, this);
	}

	public Tile getTile(int idA, int idB) {
		return tile[idA][idB];
	}

	public void setTileSize(int change) {
		TILE_SIZE += change;
		player.setPosition();
	}

	public int[] getMapPosition() {
		return mapPosition;
	}

	public void updateMapPosition(int x, int y) {
		mapPosition[0] += x;
		mapPosition[1] += y;
	}

	public void setMapPosition(int x, int y) {
		mapPosition[0] = x;
		mapPosition[1] = y;
	}

	public Player player() {
		return player;
	}

	public int[] playerPosition() {
		return new int[] {mapPosition[0]+(player.getTile().getLocalX()*TILE_SIZE),
				mapPosition[1]+(player.getTile().getLocalY()*TILE_SIZE)};
	}

	public void loadRegion(Region r, char direction) {
		/*
		 * TODO: shit with the region
		 */
		/*
		 * Randomize tiles for now
		 */
		fade[0] = true;
		Fade fade = new Fade(this, false, direction);
		fade.start();
	}

	public void finishLoading(char direction) {
		tile = map.getRegion(1, 0).getLayer(0).getTiles();
		player.setLayer(map.getRegion(1, 0).getLayer(0));
		
		switch (direction) {
		case 'l':
			player.setTile(TILES-1,
					player.getTile().getLocalY());
			setMapPosition((-TILES*TILE_SIZE)+getWidth(),
					mapPosition[1]);
			break;
		case 'r':
			player.setTile(0,
					player.getTile().getLocalY());
			setMapPosition(0,
					mapPosition[1]);
			break;
		case 'u':
			player.setTile(player.getTile().getLocalX(),
					TILES-1);
			setMapPosition(mapPosition[0],
					(-TILES*TILE_SIZE)+getHeight());
			break;
		case 'd':
			player.setTile(player().getTile().getLocalX(),
					1);
			setMapPosition(mapPosition[0],
					0);
			break;
		}
		Fade fade = new Fade(this, true, direction);
		fade.start();
	}

	public Object[] getFade() {
		return fade;
	}

	public void setFade(boolean state, int alpha) {
		fade = new Object[] {state, alpha};
	}

}